package com.zhaoshixin.rabbit.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.*;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class BizConsumer {

    @RabbitListener(queues = "bizQueue1")
    public void receiveBizMsg1(Message message, Channel channel) throws IOException {
        try {
//            int i = 1/0;
            System.out.println("收到消息1：" + new String(message.getBody()));
            //消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            System.out.println(1111111111);
            //出现异常,拒收消息,消息会进入死信队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }


    @RabbitListener(queues = "bizQueue2")
    public void receiveBizMsg2(Message message, Channel channel) throws IOException {
        System.out.println("收到消息2：" + new String(message.getBody()));
        //消息确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}